package com.gopay.webtest.controller.cloud;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.handler.cloud.TransCloudSignHandler;
import com.gopay.webtest.util.DateUtils;
import com.gopay.webtest.vo.cloud.Cloud0622Vo;
import com.gopay.webtest.xml.XmlDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 云账户对账文件下载
 * sh.jiao
 * 2017年2月21日
 * www.gopay.com.cn Inc.All rights reserved.
 */
@RequestMapping("/cloud/downloadFile")
@Controller
public class CloudDownloadFileController extends BaseController {

    @Resource(name = "xmlDao")
    private XmlDao xmlDao;

    @Resource(name="transCloudSignHandler")
    private TransCloudSignHandler transCloudSignHandler;

    @RequestMapping("/apply")
    public ModelAndView apply() {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, String> map = xmlDao.readData4Map("cloud_apply", "downloadFile");
            map.put("reqTime", DateUtils.format(new Date(), "yyyyMMddHHmmss"));
            map.put("merOrderNum", System.currentTimeMillis()+"");
            mv = new ModelAndView("cloud.downloadFile.apply");
            mv.addObject("config", map);
        } catch (Exception e) {
            logger.error("跳转页面", e);
        }

        return mv;
    }

    @RequestMapping("/doApply")
    public ModelAndView doApply(@ModelAttribute Cloud0622Vo cloud0622Vo) {
        ModelAndView mv = new ModelAndView("cloud.downloadFile.doApply");
        try {
            // 签名
            transCloudSignHandler.sign(cloud0622Vo.getTranCode()+"_"+cloud0622Vo.getVersion(), "signType", "signValue", cloud0622Vo, getRequest());
            mv.addObject("applyVo", cloud0622Vo);
        } catch (Exception e) {
            logger.error("执行申请", e);
        }
        return mv;
    }
}
